package com.thunder.soft.ui.fragment;

import android.widget.Toast;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.thunder.soft.R;
import com.thunder.soft.bean.BannerBean;
import com.thunder.soft.bean.HomeDataBean;
import com.thunder.soft.common.base.BaseFragment;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.FragmentHomeBinding;
import com.thunder.soft.ui.adapter.HomeArticleAdapter;
import com.thunder.soft.vm.HomeViewModel;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;

import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    private HomeViewModel homeViewModel;
    HomeArticleAdapter homeArticleAdapter;

    @Override
    protected void initData() {
        setTitleBar(binding.homeToolbar);

        homeViewModel = ViewModelProviderHelper.buildViewModel(this, HomeViewModel.class);
        getLifecycle().addObserver(homeViewModel);

        homeArticleAdapter = new HomeArticleAdapter();
        homeArticleAdapter.setHomeViewModel(homeViewModel);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        binding.homeRecycler.setLayoutManager(linearLayoutManager);

        binding.homeRecycler.setAdapter(homeArticleAdapter);

        binding.refreshLayout.autoRefresh();
        binding.refreshLayout.setOnRefreshListener(refresh -> homeViewModel.refreshData(true));
        binding.refreshLayout.setOnLoadMoreListener(refresh -> homeViewModel.refreshData(false));

        homeViewModel.getErrorMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
            }
        });

        homeViewModel.getBannerLiveData().observe(this, new Observer<List<BannerBean>>() {
            @Override
            public void onChanged(List<BannerBean> bannerBeans) {
                BannerImageAdapter<BannerBean> bannerImageAdapter = new BannerImageAdapter<BannerBean>(bannerBeans) {
                    @Override
                    public void onBindView(BannerImageHolder holder, BannerBean data, int position, int size) {
                        Glide.with(getContext())
                                .load(data.getImagePath())
                                .into(holder.imageView);
                    }
                };
                binding.homeBanner.setAdapter(bannerImageAdapter);
            }
        });

        homeViewModel.getHomeDataLiveData().observe(this, new Observer<HomeDataBean>() {
            @Override
            public void onChanged(HomeDataBean homeDataBeans) {
                if (homeDataBeans.getDatas().size() < 20) {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData();
                }
                if (homeDataBeans.getCurPage() == 1) {
                    binding.refreshLayout.finishRefresh();
                    homeArticleAdapter.addListClear(homeDataBeans.getDatas());
                } else {
                    homeArticleAdapter.addList(homeDataBeans.getDatas());
                    binding.refreshLayout.finishLoadMore();
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }
}